package cn.highedu.coolsharkhub.controller;

import cn.highedu.coolsharkhub.pojo.dto.ProductDTO;
import cn.highedu.coolsharkhub.pojo.vo.ProductManagementVO;
import cn.highedu.coolsharkhub.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProductController {
    private Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;

    @GetMapping("/api/products")
    public List<ProductManagementVO> getProducts() {
        return productService.findAllForManagement();
    }

    @PostMapping("/api/products")
    @ResponseStatus(HttpStatus.CREATED)
    public void addProduct(@RequestBody ProductDTO product) {
        productService.add(product);
    }

    @PutMapping("/api/products/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updateProduct(@PathVariable Integer id, @RequestBody ProductDTO product) {
        productService.updateById(id, product);
    }

    @DeleteMapping("/api/products/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteProduct(@PathVariable Integer id) {
        productService.deleteById(id);
    }
}
